//
//  KFChoseBoardVC.m
//  KafeeSale
//
//  Created by Liboom on 2025/6/16.
//

#import "KFChoseBoardVC.h"

@interface KFChoseBoardVC ()
@property(nonatomic, strong) NSArray *dataList;
@property (nonatomic, strong) NSArray *groupedDataArray;
@end

@implementation KFChoseBoardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTitle = @"选择桌台";
    [self getData];
}
-(void)getData{
    NSString * url = [WorkUrl returnURL:Interface_For_Get_Free_Board];
    XHQHUDInView(self.view);
    [PGNetworkHelper GET:url parameters:@{@"notInBoardId":@(self.boardId)} cache:NO headers:[Mytools getHeaders] responseCache:nil success:^(id responseObject) {
        XHQHUDHide();
        if ([responseObject[@"code"] integerValue]==200) {
            self.dataList = responseObject[@"data"];
            [self createUI];
        }else{
            XHQHUDErrorText(responseObject[@"msg"]);
        }
    } failure:^(id error) {
        XHQHUDHide();
    }];
}
-(void)createUI{
    UIScrollView * bgScro = [[UIScrollView alloc]initWithFrame:Frame(0 , kTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT - kTopHeight-kTabbarHeight)];
    [self.view addSubview:bgScro];
    
    NSInteger btnW = (SCREEN_WIDTH - 180)/5;
    NSInteger btnH = btnW;
    UIButton * lastBtn = nil;
    
    // 创建分组数组（增加户外分组）
    NSMutableArray *arr1 = [NSMutableArray new];  // 桌
    NSMutableArray *arr2 = [NSMutableArray new];  // 吧
    NSMutableArray *arr3 = [NSMutableArray new];  // 土和意（土在前，意在后）
    NSMutableArray *arr4 = [NSMutableArray new];  // 户外
    NSMutableArray *arr5 = [NSMutableArray new];  // 其他（保留原有结构）
    
    // 收集所有土和意到临时数组
    NSMutableArray *soilArray = [NSMutableArray new];
    NSMutableArray *meaningArray = [NSMutableArray new];
    
    for (int i=0; i<self.dataList.count; i++) {
        NSDictionary *dic = self.dataList[i];
        NSString *boardName = dic[@"boardName"];
        
        if ([boardName containsString:@"桌"]) {
            [arr1 addObject:dic];
        } else if ([boardName containsString:@"吧"]) {
            [arr2 addObject:dic];
        } else if ([boardName containsString:@"土"]) {
            [soilArray addObject:dic];
        } else if ([boardName containsString:@"意"]) {
            [meaningArray addObject:dic];
        } else if ([boardName containsString:@"户"]) { // 新增户外处理
            [arr4 addObject:dic];
        } else {
            [arr5 addObject:dic]; // 其他未分类项
        }
    }
    
    // 合并土和意到arr3
    [arr3 addObjectsFromArray:soilArray];
    [arr3 addObjectsFromArray:meaningArray];
    
    // 更新分组数组（包含户外分组）
    self.groupedDataArray = @[arr1, arr2, arr3, arr4, arr5];
    
    for (int i=0; i<self.groupedDataArray.count; i++) {
        NSMutableArray * array = self.groupedDataArray[i];
        for (int j =0; j<array.count; j++) {
            NSDictionary *dic = array[j];
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = Frame(50+(j%5)*(btnW+20), 50+i*(btnH+50), btnW, btnH);
            btn.backgroundColor = UIColor.whiteColor;
            btn.borderColor = UIColor.blackColor;
            btn.borderWidth = 1;
            [btn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
            [btn setTitle:dic[@"boardName"] forState:UIControlStateNormal];
            btn.titleLabel.font = UIFontMake(17);
            btn.tag = 10*i+j;
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.cornerRadius = btnW/2;
            [bgScro addSubview:btn];
            lastBtn = btn;
            
        }
    }
    bgScro.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(lastBtn.frame)+20);
    
}
-(void)btnClick:(UIButton*)sender{
    NSInteger tag = sender.tag;

    // 计算分组索引和分组内的索引
    NSInteger section = tag / 10;  // 分组索引 (i)
    NSInteger index = tag % 10;    // 分组内的索引 (j)

    // 确保分组索引有效
    if (section < self.groupedDataArray.count) {
        NSArray *sectionArray = self.groupedDataArray[section];
        
        // 确保分组内的索引有效
        if (index < sectionArray.count) {
            NSDictionary *b = sectionArray[index];
            XHQHUDInView(self.view);
            [PGNetworkHelper POST:[WorkUrl returnURL:Interface_For_Post_Change_Board] parameters:@{@"orderId":@(self.orderId),@"changeBoardId":b[@"id"]} cache:NO headers:[Mytools getHeaders] responseCache:nil success:^(id responseObject) {
                XHQHUDHide();
                if ([responseObject[@"code"] integerValue]==200) {
                    XHQHUDSuccessText(@"换台成功");
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    XHQHUDErrorText(responseObject[@"msg"]);
                }
            } failure:^(id error) {
                XHQHUDHide();
            }];
        }
    }
    
}

@end
